<?php
include '../c/config.php';
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);

$query=$mysqli->query("select id from z_game where is_open='1' limit 0, 1");
if($query->num_rows>0){
    $getData=$query->fetch_assoc();
    $ver=$getData['id'];
}else{
    exit;
}
//create users;
$table='y'.$ver.'_users';
file_put_contents('/var/www/c/config.txt', $ver);
if ($result = $mysqli->query("SHOW TABLES LIKE '".$table."'")) {
    if($result->num_rows == 1) {
        echo "Table exists";
        exit;
    }
}else {
    echo "Table does not exist";
    echo "<br/>";
}
$mysqli->query("START TRANSACTION");
$sql=<<<EOT
CREATE TABLE `{$table}` (
  `id` int NOT NULL,
  `phone` varchar(40) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `name` varchar(120) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `headimg` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `openid` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `province` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `city` varchar(40) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `beizhu` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `createtime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
EOT;
$mysqli->query($sql);
$mysqli->query("ALTER TABLE `".$table."` ADD PRIMARY KEY (`id`) ");
$mysqli->query("ALTER TABLE `".$table."` MODIFY `id` int NOT NULL AUTO_INCREMENT");

// --- end create users


//create puv;
$table='y'.$ver.'_puv';
$sql=<<<EOT
CREATE TABLE `{$table}` (
  `id` int NOT NULL,
  `openid` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `theday` varchar(15) COLLATE utf8mb4_general_ci NOT NULL,
  `createtime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
EOT;
$mysqli->query($sql);
$mysqli->query("ALTER TABLE `".$table."` ADD PRIMARY KEY (`id`) ");
$mysqli->query("ALTER TABLE `".$table."` MODIFY `id` int NOT NULL AUTO_INCREMENT");
// --- end create puv


//create fen;
$table='y'.$ver.'_fen';
$sql=<<<EOT
CREATE TABLE `{$table}` (
  `id` int NOT NULL,
  `openid` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `fen` int NOT NULL DEFAULT '0',
  `beizhu` varchar(40) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `ishuojiang` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `createtime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
EOT;
$mysqli->query($sql);
$mysqli->query("ALTER TABLE `".$table."` ADD PRIMARY KEY (`id`) ");
$mysqli->query("ALTER TABLE `".$table."` MODIFY `id` int NOT NULL AUTO_INCREMENT");
// --- end create fen



//create group;
$table='y'.$ver.'_group';
$sql=<<<EOT
CREATE TABLE `{$table}` (
  `id` int NOT NULL,
  `openid` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `tid` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `headimg` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `createtime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
EOT;
$mysqli->query($sql);
$mysqli->query("ALTER TABLE `".$table."` ADD PRIMARY KEY (`id`) ");
$mysqli->query("ALTER TABLE `".$table."` MODIFY `id` int NOT NULL AUTO_INCREMENT");
// --- end create group



//create group;
$table='y'.$ver.'_pvuv';
$sql=<<<EOT
CREATE TABLE `{$table}` (
  `id` int NOT NULL,
  `pv` int NOT NULL DEFAULT '0',
  `uv` int NOT NULL DEFAULT '0',
  `theday` varchar(40) COLLATE utf8mb4_general_ci NOT NULL,
  `createtime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
EOT;
$mysqli->query($sql);
$mysqli->query("ALTER TABLE `".$table."` ADD PRIMARY KEY (`id`) ");
$mysqli->query("ALTER TABLE `".$table."` MODIFY `id` int NOT NULL AUTO_INCREMENT");
// --- end create group



//create team;
$table='y'.$ver.'_team';
$sql=<<<EOT
CREATE TABLE `{$table}` (
  `id` int NOT NULL,
  `openid` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `name` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `fen` int NOT NULL DEFAULT '0',
  `iswork` int NOT NULL DEFAULT '1',
  `renshu` int NOT NULL DEFAULT '0',
  `ishuojiang` int DEFAULT NULL,
  `createtime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
EOT;
$mysqli->query($sql);
$mysqli->query("ALTER TABLE `".$table."` ADD PRIMARY KEY (`id`) ");
$mysqli->query("ALTER TABLE `".$table."` MODIFY `id` int NOT NULL AUTO_INCREMENT");
// --- end create team


$mysqli->query("COMMIT");
$mysqli->close();
echo 1;

?>